<template>
  <h1>{{ msg }}</h1>

  <p>
    <a href="https://vitejs.dev/guide/features.html" target="_blank">
      Vite Documentation
    </a>
    |
    <a href="https://v3.vuejs.org/" target="_blank">{{msg2}}</a>
  </p>

  <button type="button" @click="state.count++" class="p-3 py-2 rounded-md bg-red-500 hover:bg-red-700">
    count is: {{ state.count }}
  </button>
  <p>
    {{ state.doubleCounter }}
    <code>components/HelloWorld.vue</code> to test hot module replacement.
  </p>
</template>

<script setup>
import { computed,  reactive, onMounted,onUnmounted,ref } from 'vue'
//import { XrmServiceToolkit } from  'XrmServiceTool'
import { XrmServiceToolkit } from  '../xrmhelper/XrmServiceToolkit'

defineProps({
  msg: String
})

 //counter
 const state = useCounter();

//message
const msg2 = ref(import.meta.env.DEV);

  
</script>

<script>
function useCounter(){
  const state = reactive({ 
          count: 0,
          doubleCounter: computed(()=>{
           return state.count*2
          }) 
  })

  let timer 
  onMounted(()=>{
    timer = setInterval(()=>{

      state.count++
    },1000)
    
    
    if(window.Xrm){
     
     const Xrm= window.Xrm;
     let crmServerUrl = Xrm.Page.context.getClientUrl();
     XrmServiceToolkit.Rest.RetrieveMultiple("AccountSet",
     "?$select=AccountId&$top=50",(result)=>{
        Xrm.Utility.alertDialog(JSON.stringify(result));
     },(error)=>{
       Xrm.Utility.alertDialog(error.message);
     },()=>{

     },true)
     console.log(crmServerUrl);
    }

    let systemuserid =XrmServiceToolkit.Soap.GetCurrentUserId();
    console.log(systemuserid);
  
     

  })
  
  onUnmounted(()=>{
    clearInterval(timer)
  })
  return state
}
export default {
  
}
</script>


<style scoped>
a {
  color: #42b983;
}
</style>
